package com.net.oil.oilWear.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.oilWear.bean.FillOilRecordBean;
import com.net.oil.util.DateFormat;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：lch
 * 时间：2019/1/19 0019:下午 20:35
 * 邮箱：245850413@qq.com
 * 说明：
 */
public class FillOilRecordRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_FOOTER = 0;  //底部FootView
    private static final int TYPE_ITEM = 1;  //普通item

    private int load_more_status = 10;      //上拉加载更多状态-默认为10

    private Context context;
    private LayoutInflater inflater;

    private List<FillOilRecordBean.ListBean> list;

    private OnItemClickListener onItemClickListener;

    public FillOilRecordRecyclerViewAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        list = new ArrayList<>();
    }

    public void updateList(List<FillOilRecordBean.ListBean> list) {
        if (list == null) {
            list = new ArrayList<>();
        }
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    public void loadMore(List<FillOilRecordBean.ListBean> list) {
        if (list == null) return;
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    public void  removeItem(int position){
        list.remove(position);
        notifyItemRemoved(position);
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case TYPE_FOOTER:
                view = inflater.inflate(R.layout.recycler_view_foot_item, parent, false);
                return new FootViewHolder(view);
            case TYPE_ITEM:
                view = inflater.inflate(R.layout.fill_oil_record_recycler_view_item, parent, false);
                return new ViewHolder(view);
            default:
                return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            bindItemView(viewHolder, position);
        } else if (holder instanceof FootViewHolder) {
            FootViewHolder footViewHolder = (FootViewHolder) holder;
            footDataBind(footViewHolder);
        }
    }

    private void bindItemView(final ViewHolder viewHolder, final int position) {
        final FillOilRecordBean.ListBean bean = list.get(viewHolder.getAdapterPosition());
        /* 加油升数 */
        viewHolder.fillOilAmountTv.setText(bean.getQuantity() + "升");
        /* 加油时间 yyyy-mm-dd */
        viewHolder.fillOilDateTv.setText(DateFormat.formatDateTime(bean.getT(), DateFormat.FormatFlag.YDM));
        /* 加油花费的总金额 */
        viewHolder.fillOilMoneyTv.setText(String.valueOf(bean.getOilMoney()));
        /* 里程 */
        viewHolder.mileageTv.setText(String.valueOf(bean.getCurrentMileage()));
        /* 加油油价 */
        viewHolder.oilPriceTv.setText(bean.getOilPrice() + "元/升");

        viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                onItemClickListener.onItemLongClickListener(position, bean.getId());
                return true;
            }
        });

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClickListener(bean);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    /**
     * 更改滑动的状态
     *
     * @param status 上拉加载更多 PULL_LOAD_MORE = 10; 正在加载中 LOADING_MORE = 11;
     *               没有更多 LOADING_NONE = 12; 上拉加载更多状态-默认为10
     */
    public void changeMoreStatus(int status) {
        load_more_status = status;
        notifyDataSetChanged();
    }

    /**
     * 绑定加载更多项的数据
     *
     * @param footViewHolder holder
     */
    private void footDataBind(FootViewHolder footViewHolder) {
        switch (load_more_status) {
            case Constant.PULL_LOAD_MORE:
                footViewHolder.itemView.setVisibility(View.VISIBLE);
                footViewHolder.more.setText("上拉加载更多...");
                footViewHolder.more.setEnabled(false);
                footViewHolder.progressBar.setVisibility(View.GONE);
                break;
            case Constant.LOADING_MORE:
                footViewHolder.itemView.setVisibility(View.VISIBLE);
                footViewHolder.more.setText("正在加载更多数据...");
                footViewHolder.more.setEnabled(false);
                footViewHolder.progressBar.setVisibility(View.VISIBLE);
                break;
            case Constant.LOADING_NONE:
                footViewHolder.itemView.setVisibility(View.VISIBLE);
                footViewHolder.more.setText("没有更多了");
                footViewHolder.more.setEnabled(false);
                footViewHolder.progressBar.setVisibility(View.GONE);
                break;
            case Constant.LOADING_FAIL:
                footViewHolder.more.setText("加载失败");
                footViewHolder.more.setEnabled(true);
                footViewHolder.progressBar.setVisibility(View.GONE);
                break;
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.fill_oil_date_tv)
        TextView fillOilDateTv;
        @BindView(R.id.fill_oil_money_tv)
        TextView fillOilMoneyTv;
        @BindView(R.id.mileage_tv)
        TextView mileageTv;
        @BindView(R.id.oil_price_tv)
        TextView oilPriceTv;
        @BindView(R.id.fill_oil_amount_tv)
        TextView fillOilAmountTv;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    static class FootViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.progress_bar)
        ProgressBar progressBar;
        @BindView(R.id.more)
        TextView more;

        FootViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {

        void onItemLongClickListener(int position, int recordId);

        void onItemClickListener(FillOilRecordBean.ListBean listBean);
    }
}
